﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class ExportDeclarationsBLL
    {
        private static readonly IAttachDA attachDA = DataAccess.CreateAttachDA();
        private static readonly IAttachDetailDA attachDetailDA = DataAccess.CreateAttachDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IExportDeclarationsDA exportDeclarationsDA = DataAccess.CreateExportDeclarationsDA();
        private static readonly IExportDeclarationsDetailDA exportDeclarationsDetailDA = DataAccess.CreateExportDeclarationsDetailDA();
        private static readonly IItemNoDA itemNoDA = DataAccess.CreateItemNoDA();
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private static readonly IOrdersDA ordersDA = DataAccess.CreateOrdersDA();
        private PubParasInfo pubParasInfo;
        private static readonly IRotationRuleDA rotataionRuleDA = DataAccess.CreateRotationRuleDA();
        private static readonly IRotationRuleDetailDA rotataionRuleDetailDA = DataAccess.CreateRotationRuleDetailDA();
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();
        private static readonly ISysSettingDA sysSettingDA = DataAccess.CreateSysSettingDA();

        public ExportDeclarationsBLL()
        {
            this.pubParasInfo = null;
        }

        public ExportDeclarationsBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public ExportDeclarationsBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDeclarationsInfo(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                string DID = IDS[0];
                ExportDeclarationsInfo exportDeclarationsInfo = exportDeclarationsDA.GetExportDeclarationsByID(dataBase, tran, DID, this.dataBaseID[0], this.pubParasInfo);
                exportDeclarationsInfo.exportDeclarationsDetailInfos = exportDeclarationsDetailDA.GetExportDeclarationsDetailByID(dataBase, tran, DID, this.dataBaseID[0]);
                foreach (string id in IDS)
                {
                    ExportDeclarationsInfo info = exportDeclarationsDA.GetExportDeclarationsByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    if (info.DeclarationsType.ToUpper() == "CR")
                    {
                        this.UpdateExternalIDByDID(info.DeclarationsID, "DELETE", info.OwnerID, info.OrderID, this.pubParasInfo);
                    }
                }
                DID = DID + "EXPORTD";
                List<AttachDetailInfo> attachDetailInfos = attachDetailDA.GetAttachDetailListByID(DID, this.dataBaseID[0]);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (exportDeclarationsDA.DeleteExportDeclarations(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    auditDataBLL.InsertAuditData<ExportDeclarationsInfo>(dataBase, tran, exportDeclarationsInfo, null, AuditAction.Delete, this.pubParasInfo.UserID, "EXPORT_DECLARATIONS", this.dataBaseID[0]);
                    if ((exportDeclarationsInfo.exportDeclarationsDetailInfos.Count > 0) && (exportDeclarationsDetailDA.DeleteExportDeclarationsDetail(dataBase, tran, IDS, this.dataBaseID[0]) > 0))
                    {
                        for (int i = 0; i < exportDeclarationsInfo.exportDeclarationsDetailInfos.Count; i++)
                        {
                            OrdersInfo ordersinfo = ordersDA.GetOrdersByID(dataBase, tran, exportDeclarationsInfo.exportDeclarationsDetailInfos[i].OrderID, this.dataBaseID[0], this.pubParasInfo);
                            orderDetailDA.UpdateOrderDeclarationQtyByOrderIDLineID(dataBase, tran, exportDeclarationsInfo.exportDeclarationsDetailInfos[i].OrderID, exportDeclarationsInfo.exportDeclarationsDetailInfos[i].OrderLineID.ToString(), exportDeclarationsInfo.exportDeclarationsDetailInfos[i].Qty, "DELETE", this.pubParasInfo.UserID, this.dataBaseID[0]);
                            auditDataBLL.InsertAuditData<ExportDeclarationsDetailInfo>(dataBase, tran, exportDeclarationsInfo.exportDeclarationsDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "EXPORT_DECLARATIONS", this.dataBaseID[0]);
                        }
                    }
                    if (attachDetailInfos.Count > 0)
                    {
                        attachDetailDA.DeleteAttachDetail(DID, this.dataBaseID[0]);
                        IDS[0] = DID;
                        attachDA.DeleteAttach(IDS, this.dataBaseID[0]);
                    }
                    for (int n = 0; IDS.Count > n; n++)
                    {
                        temp = temp + IDS[n] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllDeclarationsInfo()
        {
            return exportDeclarationsDA.GetAllExportDeclarations(this.dataBaseID[0]);
        }

        public ExportDeclarationsInfo GetDeclarationsInfoByID(string id)
        {
            ExportDeclarationsInfo exportDeclarationsInfo = exportDeclarationsDA.GetExportDeclarationsByID(id, this.dataBaseID[0], this.pubParasInfo);
            if (exportDeclarationsInfo != null)
            {
                exportDeclarationsInfo.exportDeclarationsDetailInfos = exportDeclarationsDetailDA.GetExportDeclarationsDetailGroupBy(id, this.dataBaseID[0]);
                exportDeclarationsInfo.AttachInfos = attachDA.GetAttachByID(id + "EXPORTD", this.dataBaseID[0]);
                if (exportDeclarationsInfo.AttachInfos != null)
                {
                    exportDeclarationsInfo.AttachInfos.AttachDetailInfos = attachDetailDA.GetAttachDetailListByID(id + "EXPORTD", this.dataBaseID[0]);
                }
            }
            return exportDeclarationsInfo;
        }

        public DataSet GetDeclarationsInfoByQueryEntity(ExportDeclarationsQueryEntity exportDeclarationsQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(exportDeclarationsQueryEntity.DeclarationsID))
            {
                sqlWhere.Add(" EDS.DECLARATIONS_ID " + exportDeclarationsQueryEntity.DeclarationsID);
            }
            if (!string.IsNullOrEmpty(exportDeclarationsQueryEntity.OwnerID))
            {
                sqlWhere.Add(" EDS.OWNER_ID " + exportDeclarationsQueryEntity.OwnerID);
            }
            if (!string.IsNullOrEmpty(exportDeclarationsQueryEntity.DeclarationsType))
            {
                sqlWhere.Add(" EDS.DECLARATIONS_TYPE " + exportDeclarationsQueryEntity.DeclarationsType);
            }
            if (!string.IsNullOrEmpty(exportDeclarationsQueryEntity.OrderID))
            {
                sqlWhere.Add(" EDS.ORDER_ID " + exportDeclarationsQueryEntity.OrderID);
            }
            if (!string.IsNullOrEmpty(exportDeclarationsQueryEntity.DeclarationsDate))
            {
                sqlWhere.Add(exportDeclarationsQueryEntity.DeclarationsDate.Replace("@DeclarationsDate@", "EDS.DECLARATIONS_DATE"));
            }
            if (!string.IsNullOrEmpty(exportDeclarationsQueryEntity.Status))
            {
                sqlWhere.Add("EDS.STATUS " + exportDeclarationsQueryEntity.Status);
            }
            return exportDeclarationsDA.GetExportDeclarationsByQueryList(sqlWhere, exportDeclarationsQueryEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        public string InsertDeclarationsInfo(ExportDeclarationsInfo exportDeclarationsInfo)
        {
            if (!string.IsNullOrEmpty(exportDeclarationsInfo.DeclarationsID) && exportDeclarationsDA.CheckExportDeclarationsIDUnique(exportDeclarationsInfo.DeclarationsID, this.dataBaseID[0]))
            {
                throw new Exception("此报关单号已存在!");
            }
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strDeclarationsID = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(exportDeclarationsInfo.DeclarationsID))
                {
                    exportDeclarationsInfo.DeclarationsID = commonMethod.GenNextNumStr(dataBase, tran, "EXPORT_DECLARATIONS", whLoginID);
                }
                if (exportDeclarationsInfo.DeclarationsType.ToUpper() == "CR")
                {
                    this.UpdateExternalIDByDID(exportDeclarationsInfo.DeclarationsID, "ADD", exportDeclarationsInfo.OwnerID, exportDeclarationsInfo.OrderID, this.pubParasInfo);
                }
                exportDeclarationsInfo.CreatedBy = this.pubParasInfo.UserID;
                exportDeclarationsInfo.UpdatedBy = exportDeclarationsInfo.CreatedBy;
                if (exportDeclarationsDA.InsertExportDeclarations(dataBase, tran, exportDeclarationsInfo, whLoginID) > 0)
                {
                    if (exportDeclarationsInfo.DeclarationsType == "NCR")
                    {
                        SysSettingInfo sysSettingInfo = sysSettingDA.GetSysSettingByID("UpdateByCustomsTheOtORIn", whLoginID, this.pubParasInfo);
                        if ((sysSettingInfo != null) && (sysSettingInfo.Value == "Y"))
                        {
                            ordersDA.UpdateOrdersExternOrderIDByOrderID(exportDeclarationsInfo.OrderID, exportDeclarationsInfo.DeclarationsID, whLoginID, this.pubParasInfo);
                        }
                    }
                    if ((exportDeclarationsInfo.exportDeclarationsDetailInfos != null) && (exportDeclarationsInfo.exportDeclarationsDetailInfos.Count > 0))
                    {
                        foreach (ExportDeclarationsDetailInfo DetailInfo in exportDeclarationsInfo.exportDeclarationsDetailInfos)
                        {
                            DetailInfo.DeclarationsID = exportDeclarationsInfo.DeclarationsID;
                            DetailInfo.CreatedBy = this.pubParasInfo.UserID;
                            DetailInfo.UpdatedBy = DetailInfo.CreatedBy;
                            exportDeclarationsDetailDA.InsertExportDeclarationsDetail(DetailInfo, whLoginID);
                        }
                    }
                    strDeclarationsID = exportDeclarationsInfo.DeclarationsID;
                    new AuditDataBLL().InsertAuditData<ExportDeclarationsInfo>(dataBase, tran, null, exportDeclarationsInfo, AuditAction.Insert, this.pubParasInfo.UserID, "EXPORT_DECLARATIONS", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strDeclarationsID;
        }

        public ExportDeclarationsInfo OrderIDChanged(string orderID, string type, string ownerID, PubParasInfo pubParasInfo)
        {
            List<OrderDetailInfo> orderDetailinfo = new List<OrderDetailInfo>();
            string DeclarationsID = "";
            if (type != "CR")
            {
                OrdersInfo ordersInfo = new OrdersInfo();
                ordersInfo = ordersDA.GetOrdersByID(orderID, this.dataBaseID[0], pubParasInfo);
                if ((ordersInfo == null) || !(ordersInfo.OrderID != ""))
                {
                    throw new Exception("出库单:" + orderID + "不存在!");
                }
                if (!(ordersInfo.DeclarationsType == "NCR"))
                {
                    throw new Exception("出库单:" + orderID + "报关类型不是非集报,请重新输入或选择入库单!");
                }
                if (ordersInfo.IsDeclartion == "Y")
                {
                    throw new Exception("出库单:" + orderID + "无法重复报关,请重新输入!");
                }
                if (!(!(ordersInfo.OwnerID != ownerID) || string.IsNullOrEmpty(ownerID)))
                {
                    throw new Exception("出库单:" + orderID + "与货主不匹配,请重新输入!");
                }
                if ((ordersInfo.Status != "Shipped") && (ordersInfo.Status != "Closed"))
                {
                    throw new Exception("出库单:" + orderID + "状态不等于 发货完成或者 关闭 无法进行报关操作!");
                }
                DeclarationsID = ordersInfo.ExternOrderID;
                orderDetailinfo = orderDetailDA.GetOrderDetailListByOrderID(orderID, this.dataBaseID[0], pubParasInfo);
            }
            else
            {
                List<OrdersInfo> ordersInfos = ordersDA.GetOrdersByCRID(orderID, ownerID, this.dataBaseID[0], pubParasInfo);
                if ((ordersInfos == null) || (ordersInfos.Count <= 0))
                {
                    throw new Exception("核放单号:" + orderID + "未找到匹配的数据源!");
                }
                foreach (OrdersInfo info in ordersInfos)
                {
                    info.OrderDetailInfos = orderDetailDA.GetOrderDetailListByOrderID(info.OrderID, this.dataBaseID[0], pubParasInfo);
                    if ((info.OrderDetailInfos != null) && (info.OrderDetailInfos.Count > 0))
                    {
                        foreach (OrderDetailInfo detailinfo in info.OrderDetailInfos)
                        {
                            orderDetailinfo.Add(detailinfo);
                        }
                    }
                }
            }
            List<ExportDeclarationsDetailInfo> exportDeclarationsDetailInfos = new List<ExportDeclarationsDetailInfo>();
            if ((orderDetailinfo != null) && (orderDetailinfo.Count > 0))
            {
                for (int i = 0; orderDetailinfo.Count > i; i++)
                {
                    if ((orderDetailinfo[i].ShippedQty - orderDetailinfo[i].DeclarationQty) > 0M)
                    {
                        SkuInfo skuInfo = skuDA.GetSkuByID(orderDetailinfo[i].OwnerID, orderDetailinfo[i].SkuID, this.dataBaseID[0]);
                        ExportDeclarationsDetailInfo exportDeclarationsDetailInfo = new ExportDeclarationsDetailInfo {
                            LineID = (i + 1) * 10,
                            OrderID = orderDetailinfo[i].OrderID,
                            OrderLineID = orderDetailinfo[i].LineID,
                            SkuID = orderDetailinfo[i].SkuID,
                            SkuDescr = orderDetailinfo[i].SkuDescr,
                            AltSkuID = orderDetailinfo[i].AltSkuID,
                            ItemNoID = orderDetailinfo[i].LotAttr03
                        };
                        if (!string.IsNullOrEmpty(orderDetailinfo[i].LotAttr03))
                        {
                            exportDeclarationsDetailInfo.ItemNoDescr = itemNoDA.GetItemNoDescrByID(orderDetailinfo[i].LotAttr03, this.dataBaseID[0]);
                        }
                        exportDeclarationsDetailInfo.OwnerID = orderDetailinfo[i].OwnerID;
                        exportDeclarationsDetailInfo.CaseID = "";
                        exportDeclarationsDetailInfo.Status = "New";
                        exportDeclarationsDetailInfo.UomID = orderDetailinfo[i].UomID;
                        exportDeclarationsDetailInfo.PackID = orderDetailinfo[i].PackID;
                        exportDeclarationsDetailInfo.Cube = Convert.ToDecimal((float) (orderDetailinfo[i].Cube * Convert.ToSingle(orderDetailinfo[i].ShippedQty)));
                        exportDeclarationsDetailInfo.GrossWgt = Convert.ToDecimal((float) (orderDetailinfo[i].GrossWGT * Convert.ToSingle(orderDetailinfo[i].ShippedQty)));
                        exportDeclarationsDetailInfo.NetWgt = Convert.ToDecimal((float) (orderDetailinfo[i].NetWGT * Convert.ToSingle(orderDetailinfo[i].ShippedQty)));
                        exportDeclarationsDetailInfo.UnitPrice = Convert.ToDecimal(orderDetailinfo[i].UnitPrice);
                        exportDeclarationsDetailInfo.ExtendedPrice = 0M;
                        exportDeclarationsDetailInfo.Udf1 = orderDetailinfo[i].Udf1;
                        exportDeclarationsDetailInfo.Udf2 = orderDetailinfo[i].Udf2;
                        exportDeclarationsDetailInfo.Udf3 = orderDetailinfo[i].Udf3;
                        exportDeclarationsDetailInfo.Udf4 = orderDetailinfo[i].Udf4;
                        exportDeclarationsDetailInfo.Udf5 = orderDetailinfo[i].Udf5;
                        exportDeclarationsDetailInfo.Udf6 = orderDetailinfo[i].Udf6;
                        exportDeclarationsDetailInfo.Udf7 = orderDetailinfo[i].Udf7;
                        exportDeclarationsDetailInfo.Udf8 = orderDetailinfo[i].Udf8;
                        exportDeclarationsDetailInfo.Udf9 = orderDetailinfo[i].Udf9;
                        exportDeclarationsDetailInfo.Udf10 = orderDetailinfo[i].Udf10;
                        exportDeclarationsDetailInfo.Remark = orderDetailinfo[i].Remark;
                        exportDeclarationsDetailInfo.CreatedBy = orderDetailinfo[i].CreatedBy;
                        exportDeclarationsDetailInfo.CreatedDate = orderDetailinfo[i].CreatedDate;
                        exportDeclarationsDetailInfo.UpdatedBy = orderDetailinfo[i].UpdatedBy;
                        exportDeclarationsDetailInfo.UpdatedDate = orderDetailinfo[i].UpdatedDate;
                        exportDeclarationsDetailInfo.Ts = orderDetailinfo[i].Ts;
                        exportDeclarationsDetailInfo.Qty = 0M;
                        exportDeclarationsDetailInfo.ShippedQty = orderDetailinfo[i].ShippedQty;
                        exportDeclarationsDetailInfo.DifferencesQty = orderDetailinfo[i].ShippedQty - orderDetailinfo[i].DeclarationQty;
                        exportDeclarationsDetailInfos.Add(exportDeclarationsDetailInfo);
                    }
                }
            }
            return new ExportDeclarationsInfo { DeclarationsID = DeclarationsID, DeclarationsType = type, exportDeclarationsDetailInfos = exportDeclarationsDetailInfos };
        }

        private string OrganizeRotationRule(RotationRuleInfo rotationRuleInfo)
        {
            string sqlOrderBy = string.Empty;
            if (rotationRuleInfo == null)
            {
                return sqlOrderBy;
            }
            if ((rotationRuleInfo.RotationRuleDetailInfo == null) || (rotationRuleInfo.RotationRuleDetailInfo.Count <= 0))
            {
                return sqlOrderBy;
            }
            sqlOrderBy = " ORDER BY ";
            for (int i = 0; i < rotationRuleInfo.RotationRuleDetailInfo.Count; i++)
            {
                string CS00002 = sqlOrderBy;
                sqlOrderBy = CS00002 + " IT." + rotationRuleInfo.RotationRuleDetailInfo[i].LotAttributes + " " + rotationRuleInfo.RotationRuleDetailInfo[i].Rotation + ",";
            }
            return sqlOrderBy.Substring(0, sqlOrderBy.Length - 1);
        }

        public string UpdateDeclarationsInfo(ExportDeclarationsInfo exportDeclarationsInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strDeclarationsID = string.Empty;
            try
            {
                exportDeclarationsInfo.UpdatedBy = this.pubParasInfo.UserID;
                ExportDeclarationsInfo oldExportDeclarationsInfo = exportDeclarationsDA.GetExportDeclarationsByID(dataBase, tran, exportDeclarationsInfo.DeclarationsID, this.dataBaseID[0], this.pubParasInfo);
                if (exportDeclarationsDA.UpdateExportDeclarations(dataBase, tran, exportDeclarationsInfo, whLoginID) > 0)
                {
                    Dictionary<string, SkuInfo> skuObjects = new Dictionary<string, SkuInfo>();
                    Dictionary<string, RotationRuleInfo> rotationRuleObjects = new Dictionary<string, RotationRuleInfo>();
                    Dictionary<string, PackInfo> packInfoObjects = new Dictionary<string, PackInfo>();
                    if ((exportDeclarationsInfo.exportDeclarationsDetailInfos != null) && (exportDeclarationsInfo.exportDeclarationsDetailInfos.Count > 0))
                    {
                        foreach (ExportDeclarationsDetailInfo DetailInfo in exportDeclarationsInfo.exportDeclarationsDetailInfos)
                        {
                            SkuInfo skuinfo = null;
                            if (!skuObjects.ContainsKey(DetailInfo.OwnerID + DetailInfo.SkuID))
                            {
                                skuinfo = skuDA.GetSkuByID(dataBase, tran, DetailInfo.OwnerID, DetailInfo.SkuID, whLoginID);
                                skuObjects.Add(DetailInfo.OwnerID + DetailInfo.SkuID, skuinfo);
                            }
                            else
                            {
                                skuObjects.TryGetValue(DetailInfo.OwnerID + DetailInfo.SkuID, out skuinfo);
                            }
                            RotationRuleInfo rotationRuleInfo = null;
                            if (!rotationRuleObjects.ContainsKey(skuinfo.RotationRuleID))
                            {
                                rotationRuleInfo = rotataionRuleDA.GetRotationRuleByID(dataBase, tran, skuinfo.RotationRuleID, this.dataBaseID[0]);
                                if (rotationRuleInfo != null)
                                {
                                    rotationRuleInfo.RotationRuleDetailInfo = rotataionRuleDetailDA.GetRotationRuleDetailListByID(dataBase, tran, rotationRuleInfo.RotationRuleID, this.dataBaseID[0]);
                                    rotationRuleObjects.Add(rotationRuleInfo.RotationRuleID, rotationRuleInfo);
                                }
                            }
                            else
                            {
                                rotationRuleObjects.TryGetValue(skuinfo.RotationRuleID, out rotationRuleInfo);
                            }
                            string orderBy = this.OrganizeRotationRule(rotationRuleInfo);
                            List<ExportDeclarationsDetailInfo> exportDeclarationsDetailList = exportDeclarationsDetailDA.GetExportDeclarationsDetailInfoByRotationRule(dataBase, tran, DetailInfo, orderBy, whLoginID);
                            if ((exportDeclarationsDetailList != null) && (exportDeclarationsDetailList.Count > 0))
                            {
                                foreach (ExportDeclarationsDetailInfo info in exportDeclarationsDetailList)
                                {
                                    List<ExportDeclarationsDetailInfo> exportDeclarationsDetail = exportDeclarationsDetailDA.GetExportDeclarationsDetailOrderIDLineID(dataBase, tran, info.OrderID, info.OrderLineID.ToString(), whLoginID);
                                    decimal qty = 0M;
                                    decimal newqty = 0M;
                                    if ((exportDeclarationsDetail != null) && (exportDeclarationsDetail.Count > 0))
                                    {
                                        foreach (ExportDeclarationsDetailInfo qtyinfo in exportDeclarationsDetail)
                                        {
                                            qty += qtyinfo.Qty;
                                        }
                                    }
                                    qty = info.ShippedQty - qty;
                                    if ((qty + info.Qty) >= DetailInfo.Qty)
                                    {
                                        newqty = DetailInfo.Qty;
                                        qty = DetailInfo.Qty;
                                        DetailInfo.Qty = 0M;
                                    }
                                    else
                                    {
                                        newqty = qty + info.Qty;
                                        DetailInfo.Qty -= newqty;
                                    }
                                    decimal extendedPrice = newqty * info.UnitPrice;
                                    exportDeclarationsDetailDA.UpdateExportDeclarationsDetailQtyByDeclarationsID(dataBase, tran, info.DeclarationsID, info.LineID.ToString(), newqty, extendedPrice, this.pubParasInfo.UserID, whLoginID);
                                    OrdersInfo ordersinfo = ordersDA.GetOrdersByID(dataBase, tran, info.OrderID, whLoginID, this.pubParasInfo);
                                    orderDetailDA.UpdateOrderDeclarationQtyByOrderIDLineID(dataBase, tran, info.OrderID, info.OrderLineID.ToString(), newqty - info.Qty, "", this.pubParasInfo.UserID, whLoginID);
                                    if (DetailInfo.Qty == 0M)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    strDeclarationsID = exportDeclarationsInfo.DeclarationsID;
                    new AuditDataBLL().InsertAuditData<ExportDeclarationsInfo>(dataBase, tran, oldExportDeclarationsInfo, exportDeclarationsInfo, AuditAction.Update, this.pubParasInfo.UserID, "EXPORT_DECLARATIONS", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strDeclarationsID;
        }

        public string UpdateDeclarationsInfoData(ExportDeclarationsInfo exportDeclarationsInfo, PubParasInfo pubParasInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strDeclarationsID = string.Empty;
            try
            {
                exportDeclarationsInfo.UpdatedBy = pubParasInfo.UserID;
                if ((exportDeclarationsDA.UpdateExportDeclarations(exportDeclarationsInfo, whLoginID) > 0) && ((exportDeclarationsInfo.exportDeclarationsDetailInfos != null) && (exportDeclarationsInfo.exportDeclarationsDetailInfos.Count > 0)))
                {
                    foreach (ExportDeclarationsDetailInfo DetailInfo in exportDeclarationsInfo.exportDeclarationsDetailInfos)
                    {
                        List<ExportDeclarationsDetailInfo> exportDeclarationsDetailInfoList = exportDeclarationsDetailDA.GetExportDeclarationsDetail(dataBase, tran, DetailInfo.DeclarationsID, DetailInfo.SkuID, whLoginID);
                        decimal i = exportDeclarationsDetailInfoList.Count;
                        if (i != 0M)
                        {
                            foreach (ExportDeclarationsDetailInfo info in exportDeclarationsDetailInfoList)
                            {
                                info.GrossWgt = DetailInfo.GrossWgt / i;
                                info.NetWgt = DetailInfo.NetWgt / i;
                                info.Cube = DetailInfo.Cube / i;
                                decimal CS00004 = decimal.Parse(DetailInfo.Udf1) / i;
                                info.Udf1 = CS00004.ToString();
                                info.Udf2 = (decimal.Parse(DetailInfo.Udf2) / i).ToString();
                                info.UnitPrice = DetailInfo.UnitPrice;
                                info.ExtendedPrice = info.Qty * DetailInfo.UnitPrice;
                                exportDeclarationsDetailDA.UpdateExportDeclarationsDetailByDeclarationsID(dataBase, tran, info, pubParasInfo.UserID, whLoginID);
                            }
                        }
                    }
                    strDeclarationsID = exportDeclarationsInfo.DeclarationsID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strDeclarationsID;
        }

        public string UpdateDeclarationsStatus(string declarationsID, string status, string verificationFormID, PubParasInfo pubParasInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strDeclarationsID = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(verificationFormID))
                {
                    exportDeclarationsDA.UpdateExportDeclarationsStatusByID(dataBase, tran, declarationsID, verificationFormID, status, pubParasInfo.UserID, whLoginID);
                }
                else
                {
                    exportDeclarationsDA.UpdateExportDeclarationsStatusByID(dataBase, tran, declarationsID, status, pubParasInfo.UserID, whLoginID);
                }
                exportDeclarationsDetailDA.UpdateExportDeclarationsDetailStatusByID(dataBase, tran, declarationsID, status, pubParasInfo.UserID, whLoginID);
                strDeclarationsID = declarationsID;
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strDeclarationsID;
        }

        private void UpdateExternalIDByDID(string declarationsID, string flag, string ownerID, string orderID, PubParasInfo pubParasInfo)
        {
            if (flag.ToUpper() == "ADD")
            {
                ordersDA.UpdateExternalOrderIDByDIDcr(declarationsID, orderID, this.dataBaseID[0], pubParasInfo);
            }
            else
            {
                List<OrdersInfo> ordersInfoList = ordersDA.GetOrdersByCRID(orderID, ownerID, this.dataBaseID[0], pubParasInfo);
                foreach (OrdersInfo info in ordersInfoList)
                {
                    if (info.ExternOrderID.Contains(declarationsID))
                    {
                        if (info.ExternOrderID == declarationsID)
                        {
                            info.ExternOrderID = info.ExternOrderID.Replace(declarationsID, "");
                        }
                        else
                        {
                            info.ExternOrderID = info.ExternOrderID.Replace(declarationsID + ",", "");
                            if (info.ExternOrderID.Contains(declarationsID))
                            {
                                info.ExternOrderID = info.ExternOrderID.Replace("," + declarationsID, "");
                            }
                        }
                        ordersDA.UpdateOrders(info, this.dataBaseID[0]);
                    }
                }
            }
        }
    }
}

